package cc.yaomm.service.basic.manager;

import cc.yaomm.common.basic.enums.StatusEnum;
import cc.yaomm.common.biz.enums.LoginTypeEnum;
import cc.yaomm.common.biz.enums.SystemEnum;
import cc.yaomm.service.basic.service.user.bo.LoginParamBO;
import cc.yaomm.service.basic.entity.user.Users;
import cc.yaomm.service.basic.mapper.user.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * Created by XQL on 2021/6/10.
 */
@Component
public class LoginManager {

    @Autowired
    private UsersMapper usersMapper;

    public Users login(LoginParamBO paramBO) {
        //查询账号信息
        List<Users> usersList = usersMapper.findLoginAccount(paramBO.getAccount(), LoginTypeEnum.ACCOUNT, SystemEnum.BIZ.getUserType());
        Assert.notEmpty(usersList, "登录失败，没有查询到账号信息");

        //判断是否锁定
        Users users = usersList.get(0);
        Assert.isTrue(StatusEnum.NORMAL.equals(users.getStatus()), "登录失败，此账号不存在或锁定");

        //判断是否账号登录
        if (paramBO.getLoginType() == LoginTypeEnum.ACCOUNT) {
            //判断密码是否正确
            String pwdMd5 = DigestUtils.md5DigestAsHex(users.getAccount().concat(paramBO.getPassword()).getBytes()).toUpperCase();
            Assert.isTrue(users.getPassword().equals(pwdMd5), "登录失败，密码错误");
        }

        return users;
    }

}
